import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { LoginUsers } from './data/user';
import Users from "./data/userdata";
import Pdca from "./data/pdca";
import Typesetupdatas from "./data/typesetupdata"
// let Users = Users;
let _Users = Users

let _Typesetupdatas = Typesetupdatas;

let _Pdca = Pdca;

export default {
  /**
   * mock bootstrap
   */
  bootstrap() {
    let mock = new MockAdapter(axios);

    // mock success request
    mock.onGet('/success').reply(200, {
      msg: 'success'
    });

    // mock error request
    mock.onGet('/error').reply(500, {
      msg: 'failure'
    });


        //登录
        mock.onPost('/user/login').reply(config => {
          console.log(config,'show me config');
          const { username } = config.body
          const token = tokens[username]
                  // mock error
        if (!token) {
          return {
            code: 60204,
            message: 'Account and password are incorrect.'
          }
        }
        return {
          code: 20000,
          data: token
        }
        });








    //登录
    mock.onPost('/login').reply(config => {
      let {username, password} = JSON.parse(config.data);
      return new Promise((resolve, reject) => {
        let user = null;
        setTimeout(() => {
          let hasUser = LoginUsers.some(u => {
            if (u.username === username && u.password === password) {
              user = JSON.parse(JSON.stringify(u));
              user.password = undefined;
              return true;
            }
          });

          if (hasUser) {
            resolve([200, { code: 200, msg: '请求成功', user }]);
          } else {
            resolve([200, { code: 500, msg: '账号或密码错误' }]);
          }
        }, 1000);
      });
    });

    //获取用户列表
    mock.onGet('/user/list').reply(config => {
      let {name} = config.params;
      let mockUsers = _Users.filter(user => {
        if (name && user.name.indexOf(name) == -1) return false;
        return true;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            users: mockUsers
          }]);
        }, 1000);
      });
    });

    //获取用户列表（分页）
    mock.onGet('/user/listpage').reply(config => {
      console.log(config.params,"show >>>>>>>>>>");
      let {page,tool,pagesize,equipmenttype} = config.params;
      let mockUsers = _Users.filter(user => {
        // if (tool && user.tool.indexOf(tool) == -1) return false;
        if(tool && equipmenttype && tool!=user.tool && equipmenttype!=user.equipmenttype) return false;
        return true;
      });
      let total = mockUsers.length;
      mockUsers = mockUsers.filter((u, index) => index < pagesize * page && index >= pagesize * (page - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            total: total,
            users: mockUsers
          }]);
        }, 1000);
      });
    });
//
        //获取TypeSetup列表（分页）
        mock.onGet('/typesetup/list').reply(config => {


          let {page,limit} = config.params;
          let mockTypesetups = _Typesetupdatas.data.list.filter(typesetupdata => {
            return true;
          });

          let totalCount = mockTypesetups.totalCount;

           mockTypesetups = mockTypesetups.filter((u, index) =>index < limit * page && index >= limit * (page - 1));
         // let mockTypesetups1 = mockTypesetups.slice(limit * (page - 1),limit * page);
          return new Promise((resolve, reject) => {
            setTimeout(() => {
              resolve([200, {
                totalCount:  _Typesetupdatas.data.totalCount,
                limit: limit,
                typesetuplist: mockTypesetups
              }]);
            }, 1000);
          });
        });




//

    //
        //获取PDCA列表（分页）
        mock.onGet('/pdca/listpage').reply(config => {
          console.log(config.params,"show >>>>>>>>>>");
          let {page,pagesize} = config.params;
          // let pagesize =5;
          let mockUsers = _Pdca.filter(user => {
            // if (tool && user.tool.indexOf(tool) == -1) return false;
            // if(false) return false;
            return true;
          });
          let total = mockUsers.length;
          mockUsers = mockUsers.filter((u, index) => index < pagesize * page && index >= pagesize * (page - 1));
          return new Promise((resolve, reject) => {
            setTimeout(() => {
              resolve([200, {
                total: total,
                users: mockUsers
              }]);
            }, 1000);
          });
        });
    //

    //删除用户
    mock.onGet('/user/remove').reply(config => {
      let { id } = config.params;
      _Users = _Users.filter(u => u.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            code: 200,
            msg: '删除成功'
          }]);
        }, 500);
      });
    });

    //批量删除用户
    mock.onGet('/user/batchremove').reply(config => {
      let { ids } = config.params;
      ids = ids.split(',');
      _Users = _Users.filter(u => !ids.includes(u.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            code: 200,
            msg: '删除成功'
          }]);
        }, 500);
      });
    });

    //编辑用户
    mock.onGet('/user/edit').reply(config => {
      let { id, name, addr, age, birth, sex } = config.params;
      _Users.some(u => {
        if (u.id === id) {
          u.name = name;
          u.addr = addr;
          u.age = age;
          u.birth = birth;
          u.sex = sex;
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            code: 200,
            msg: '编辑成功'
          }]);
        }, 500);
      });
    });

    //新增用户
    mock.onGet('/user/add').reply(config => {

      let { name, addr, age, birth, sex } = config.params;
      _Users.push({
        name: name,
        addr: addr,
        age: age,
        birth: birth,
        sex: sex
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            code: 200,
            msg: '新增成功'
          }]);
        }, 500);
      });
    });

  }
};
